package com.example.jixeishi1.controller;


import com.example.jixeishi1.dao.OrderShopMapper;
import com.example.jixeishi1.entity.CartEntity;
import com.example.jixeishi1.entity.OrderEntity;
import com.example.jixeishi1.service.CartService;
import com.example.jixeishi1.service.OrderService;
import com.example.jixeishi1.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Controller
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderShopMapper orderShopMapper;

    /**
     * 列表
     */
    @RequestMapping("/order/over")
    public ModelAndView list(){
        List<CartEntity> shoppingEntities = new ArrayList<>();
        shoppingEntities = cartService.getShopping();
        String name = "";
        for(CartEntity cartEntity :shoppingEntities){
            String shopName = cartEntity.getCmmodityName();
            name += shopName;
        }
        OrderEntity orderEntity = orderService.getOrder();
        ModelAndView mav = new ModelAndView("index(34)");
        OrderVo orderVo = new OrderVo();
        orderVo.setName(name);
        orderVo.setPrices(orderEntity.getPrices());
        mav.addObject("order" , orderVo);
        return mav;
    }



    /**
     * 保存
     */
    @PostMapping("/order/save")
    public ModelAndView save(@RequestBody OrderEntity order){
        orderService.saveOrder(order);
        ModelAndView mav = new ModelAndView("redirect:/");
        return mav;
    }

}
